package fragment.classicfragments;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ClassificationPageEntity;
import fragment.AbsBaseFragment;
import fragment.tabhostFragment.SearchFragment;
import tools.ItemOnClickListener;
import tools.ItemOnClickListenerOther;
import utils.adapter.ClassificationPageAdapter;
import volley.GsonRequest;
import volley.VolleySingleton;


/**
 * 分类 fragment
 * Created by dllo on 15/11/17.
 */
public class ClassificationPageFragment extends AbsBaseFragment implements ItemOnClickListenerOther {
    private RecyclerView recyclerView;
    private ClassificationPageAdapter adapter;
    private VolleySingleton volleyQueue;
    private String URL_PATH = "http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=android&picVersion=11&scale=2";


    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_classification;
    }

    @Override
    protected void initView() {

        recyclerView = findView(R.id.classIficationRecyclerView);

    }

    @Override
    protected void initData() {
        adapter = new ClassificationPageAdapter(getContext());
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setClassIfListener(this);
        initNetwork();
    }

    private void initNetwork() {
        volleyQueue = VolleySingleton.getInstance();
        GsonRequest<ClassificationPageEntity> request = new GsonRequest<ClassificationPageEntity>
                (URL_PATH, ClassificationPageEntity.class, new Response.Listener<ClassificationPageEntity>() {
                    @Override
                    public void onResponse(ClassificationPageEntity response) {
                        List<ClassificationPageEntity.ListEntity> list = response.getList();
                        adapter.addList(list);
                        dismissDialog();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
        volleyQueue.addRequest(request);
    }


    @Override
    public void itemListener(Object o, int i) {
        ClassificationPageEntity.ListEntity entity =
                (ClassificationPageEntity.ListEntity) o;
        goToNext(new ClassificationPageClassIfoImg(entity.getId(), entity.getTitle()));

    }


}

